#include "DXTimer.h"

/*The default constructor*/
DXTimer::DXTimer(void)
{
	/*Function that gets Counts per second*/
	QueryPerformanceFrequency((LARGE_INTEGER*)&cntsPerSec);

	/*One divided by the counts per second is how many seconds there are per count*/
	secsPerCnt = 1.0f / (float)cntsPerSec;

	/*Get the previous time stamp*/
	QueryPerformanceCounter((LARGE_INTEGER*)&prevTimeStamp);

	/*Get the current time stamp*/
	QueryPerformanceCounter((LARGE_INTEGER*)&currTimeStamp);

	/*The difference in time in milliseconds is current time stamp minus previous time stamp multiplied by the seconds per count*/
	dt = (currTimeStamp - prevTimeStamp)*secsPerCnt;

	/*Set the is timed flag to false*/
	timed = false;

	/*Set the is done flag to false*/
	isFinished = false;
}

/*The default Destructor*/
DXTimer::~DXTimer(void)
{
}

/*Function to update the difference in counts, as well as getting the current time stamp*/
void DXTimer::update()
{
	/*Get the current time stamp*/
	QueryPerformanceCounter((LARGE_INTEGER*)&currTimeStamp);

	/*The difference in time in milliseconds is current time stamp minus previous time stamp multiplied by the seconds per count*/
	dt = (currTimeStamp - prevTimeStamp)*secsPerCnt;
	
	/*Set the previous time stamp*/
	prevTimeStamp = currTimeStamp;
}

/*Function to return the difference in time stamps back*/
float DXTimer::getDT()
{
	return dt;
}

/*Function to Start and set the timer*/
void DXTimer::SetTimer()
{
	/*Set the is timed flag to true*/
	timed = true;

	/*Start the timer, and store the value*/
	startTime = (int)clock();

	/*And set the flag to is done to false*/
	isFinished = false;
}

/*Function to check to see if the timer has started*/
bool DXTimer::IsTimed()
{
	return timed;
}

/*Function to check the timer*/
bool DXTimer::CheckTimer(int value)
{
	/*Get the current time value*/
	int currTime = clock();

	/*If the current time minus the start time divided by the clocks per second is greated than the value sent in*/
	if(((currTime - startTime) / CLOCKS_PER_SEC) >= value)
	{
		/*The timer is then done*/
		timed = false;

		/*Set the start time back to 0*/
		startTime = 0;

		/*Mark the flag that proves that its done*/
		isFinished = true;

		/*Return that it has finished*/
		return true;
	}
	/*If it isn't larger than the value sent in*/
	else
	{
		/*Then the timer isn't done, or at that point*/
		return false;
	}
}